#include "Channel.h"
#include "AdminManager.h"

using namespace AgoraClient;

Channel::Channel(const unsigned int id, const unsigned int idParent, const std::string &name, const unsigned int maxNameLenght, const unsigned int maxPasswordLenght, const std::string &motd, const unsigned int maxMotdLenght, const std::string &description, const unsigned int maxDescriptionLenght, const bool visible, const unsigned int maxUser, const unsigned int maxTalker, const bool hasPassword)
: _id(id), _idParent(idParent), _parent(NULL), _name(name), _maxNameLenght(maxNameLenght), _maxPasswordLenght(maxPasswordLenght), _motd(motd), _maxMotdLenght(maxMotdLenght), _description(description), _maxDescriptionLenght(maxDescriptionLenght), _visible(visible), _maxUser(maxUser), _maxTalker(maxTalker), _hasPassword(hasPassword)
{
}

Channel::~Channel()
{

	std::list<IChannel*>::iterator it = this->_child.begin();
	std::list<IChannel*>::iterator itend = this->_child.end();

	for(; it != itend; ++it)
	{
		AdminManager::GetAdminManager()->_channelRights.erase((*it)->GetId());
		delete *it;
	}
}

const unsigned int	Channel::GetId(void) const
{
	return this->_id;
}

IChannel*	Channel::GetParent(void) const
{
	return this->_parent;
}

const unsigned int	Channel::GetIdParent(void) const
{
	return this->_idParent;
}

void	Channel::SetParent(IChannel* parent)
{
	if (this->_parent)
		this->_parent->RemoveChannelChild(this);
	this->_parent = parent;
	this->_parent->AddChannelChild(this);
	this->_idParent = parent->GetId();
}

const bool	Channel::HaveParent(void) const
{
	if (this->_parent)
		return true;
	return false;
}

const std::string&	Channel::GetName(void) const
{
	return this->_name;
}

void	Channel::SetName(const std::string& name)
{
	this->_name = name;
}

const unsigned int	Channel::GetMaxNameLength(void) const
{
	return this->_maxNameLenght;
}

void	Channel::SetMaxNameLength(const unsigned int length, bool recurcive)
{
	if (recurcive)
	{
		std::list<IChannel*>::iterator	iteCurrent = this->_child.begin();
		std::list<IChannel*>::iterator	iteEnd = this->_child.end();
		while (iteCurrent != iteEnd)
		{
			(*iteCurrent)->SetMaxNameLength(length, true);
			++iteCurrent;
		}
	}
	this->_maxNameLenght = length;
}

const std::string&	Channel::GetPassword(void) const
{
	return this->_password;
}

void	Channel::SetPassword(const std::string& password, bool recurcive)
{
	if (recurcive)
	{
		std::list<IChannel*>::iterator	iteCurrent = this->_child.begin();
		std::list<IChannel*>::iterator	iteEnd = this->_child.end();
		while (iteCurrent != iteEnd)
		{
			(*iteCurrent)->SetPassword(password, true);
			++iteCurrent;
		}
	}
	this->_password = password;
}

const unsigned int	Channel::GetMaxPasswordLength(void) const
{
	return this->_maxPasswordLenght;
}

void	Channel::SetMaxPasswordLength(const unsigned int length, bool recurcive)
{
	if (recurcive)
	{
		std::list<IChannel*>::iterator	iteCurrent = this->_child.begin();
		std::list<IChannel*>::iterator	iteEnd = this->_child.end();
		while (iteCurrent != iteEnd)
		{
			(*iteCurrent)->SetMaxPasswordLength(length, true);
			++iteCurrent;
		}
	}
	this->_maxPasswordLenght = length;
}

const std::string&	Channel::GetMotd(void) const
{
	return this->_motd;
}

void	Channel::SetMotd(const std::string& motd, bool recurcive)
{
	if (recurcive)
	{
		std::list<IChannel*>::iterator	iteCurrent = this->_child.begin();
		std::list<IChannel*>::iterator	iteEnd = this->_child.end();
		while (iteCurrent != iteEnd)
		{
			(*iteCurrent)->SetMotd(motd, true);
			++iteCurrent;
		}
	}
	this->_motd = motd;
}

const unsigned int	Channel::GetMaxMotdLength(void) const
{
	return this->_maxMotdLenght;
}

void	Channel::SetMaxMotdLength(const unsigned int length, bool recurcive)
{
	if (recurcive)
	{
		std::list<IChannel*>::iterator	iteCurrent = this->_child.begin();
		std::list<IChannel*>::iterator	iteEnd = this->_child.end();
		while (iteCurrent != iteEnd)
		{
			(*iteCurrent)->SetMaxMotdLength(length, true);
			++iteCurrent;
		}
	}
	this->_maxMotdLenght = length;
}

const std::string&	Channel::GetDescription(void) const
{
	return this->_description;
}

void	Channel::SetDescription(const std::string& description, bool recurcive)
{
	if (recurcive)
	{
		std::list<IChannel*>::iterator	iteCurrent = this->_child.begin();
		std::list<IChannel*>::iterator	iteEnd = this->_child.end();
		while (iteCurrent != iteEnd)
		{
			(*iteCurrent)->SetDescription(description, true);
			++iteCurrent;
		}
	}
	this->_description = description;
}

const unsigned int	Channel::GetMaxDescriptionLength(void) const
{
	return this->_maxDescriptionLenght;
}

void	Channel::SetMaxDescriptionLength(const unsigned int length, bool recurcive)
{
	if (recurcive)
	{
		std::list<IChannel*>::iterator	iteCurrent = this->_child.begin();
		std::list<IChannel*>::iterator	iteEnd = this->_child.end();
		while (iteCurrent != iteEnd)
		{
			(*iteCurrent)->SetMaxDescriptionLength(length, true);
			++iteCurrent;
		}
	}
	this->_maxDescriptionLenght = length;
}

const bool			Channel::GetVisibility(void) const
{
	return this->_visible;
}

void	Channel::SetVisibility(const bool visible, bool recurcive)
{
	if (recurcive)
	{
		std::list<IChannel*>::iterator	iteCurrent = this->_child.begin();
		std::list<IChannel*>::iterator	iteEnd = this->_child.end();
		while (iteCurrent != iteEnd)
		{
			(*iteCurrent)->SetVisibility(visible, true);
			++iteCurrent;
		}
	}
	this->_visible = visible;
}

const unsigned int	Channel::GetMaxUser(void) const
{
	return this->_maxUser;
}

void	Channel::SetMaxUser(const unsigned int limit, bool recurcive)
{
	if (recurcive)
	{
		std::list<IChannel*>::iterator	iteCurrent = this->_child.begin();
		std::list<IChannel*>::iterator	iteEnd = this->_child.end();
		while (iteCurrent != iteEnd)
		{
			(*iteCurrent)->SetMaxUser(limit, true);
			++iteCurrent;
		}
	}
	this->_maxUser = limit;
}

const unsigned int	Channel::GetMaxTalker(void) const
{
	return this->_maxTalker;
}

void	Channel::SetMaxTalker(const unsigned int limit, bool recurcive)
{
	if (recurcive)
	{
		std::list<IChannel*>::iterator	iteCurrent = this->_child.begin();
		std::list<IChannel*>::iterator	iteEnd = this->_child.end();
		while (iteCurrent != iteEnd)
		{
			(*iteCurrent)->SetMaxTalker(limit, true);
			++iteCurrent;
		}
	}
	this->_maxTalker = limit;
}

void	Channel::AddChannelChild(IChannel* channel)
{
	this->_child.push_back(channel);
}

void	Channel::RemoveChannelChild(IChannel* channel)
{
	this->_child.remove(channel);
}

std::list<IChannel*>*	Channel::GetChannelChild(void)
{
	return &(this->_child);
}

void	Channel::AddUser(IUser* user)
{
	std::list<IUser*>::iterator it = this->_user.begin();
	std::list<IUser*>::iterator itend = this->_user.end();
	for (; it != itend; ++it)
	{
		if ((*it)->GetId() == user->GetId())
			return;
	}
	this->_user.push_back(user);
}

void	Channel::RemoveUser(IUser* user)
{
	this->_user.remove(user);
}

std::list<IUser*>*	Channel::GetUser()
{
	return &(this->_user);
}

const bool			Channel::HasPassword(void) const
{
	return this->_hasPassword;
}

void				Channel::SetHasPassword(const bool val, bool recurcive)
{
	if (recurcive)
	{
		std::list<IChannel*>::iterator	iteCurrent = this->_child.begin();
		std::list<IChannel*>::iterator	iteEnd = this->_child.end();
		while (iteCurrent != iteEnd)
		{
			(*iteCurrent)->SetHasPassword(val, true);
			++iteCurrent;
		}
	}
	this->_hasPassword = val;
}